import logging2 as logging

_inited = False  # 初始化标志


# 初始化日志处理器
def init_logging(filename=None, fmt=None, level=logging.INFO):
    global _inited
    if _inited:
        return

    if not fmt:
        fmt = '\033[32m%(levelname)-5s [%(asctime)s] [%(thread)d] [%(filename)s=>%(funcName)s():%(lineno)d] - %(message)s'

    date_fmt = '%Y-%m-%d %H:%M:%S'

    if not filename:
        # 只使用控制台
        logging.basicConfig(level=level, datefmt=date_fmt, format=fmt)

    else:
        # 文件 + 控制台
        logging.basicConfig(level=level, datefmt=date_fmt, format=fmt, filename=filename)

        formatter = logging.Formatter(fmt, date_fmt)

        # 添加控制台日志处理
        consoleHandler = logging.StreamHandler()
        consoleHandler.setFormatter(formatter)
        consoleHandler.setLevel(level)

        logger = logging.getLogger()
        logger.addHandler(consoleHandler)

    _inited = True
